//
//  ClubMyCommitViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/18.
//

import UIKit

class ClubMyCommitViewController: ClubBaseViewController, RaeAnimationProtocol {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("我的评论").color(.black).font(16.font_semiBold)
        .alignment(.center).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubMyCommitCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 250.0.Scale
        return tableView
    }()
    private let emptyView = ClubEmptyView()
    private var dataSource:[ClubMyCommitListModel] = [ClubMyCommitListModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        loadData()
        
        view.subviews{
            backButton
            titleLabel
            tableView
            emptyView
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.centerHorizontally()
        titleLabel.CenterY == backButton.CenterY
        tableView.leading(0).trailing(0).top(.navi_height).bottom(.safe_bottom)
        emptyView.leading(0).trailing(0).height(200).top(.navi_height + 200.0.Scale)
        
        emptyView.isHidden = true
        emptyView.titleString = "你还没有发表的评论哟~"
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
    
    private func loadData(){
        
        Task {
            let res = await network.request(.myCommitList,type: ClubMyCommitListModel.self)
            if res.isSuccess, let models = res.models {
                self.emptyView.isHidden = models.count > 0
                self.dataSource = models
                self.tableView.reloadData()
            }
        }
    }
    
    private func delCommit(commitID: String){
        
        Task {
            let res = await network.request(.delMyCommit(commitID: commitID))
            if res.isSuccess {
                self.loadData()
            }
        }
    }
}


extension ClubMyCommitViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubMyCommitCell", for: indexPath) as! ClubMyCommitCell
        cell.model = dataSource[indexPath.row]
        cell.toGameDetailBlock = { gameID in
            let detailVC = ClubGameDetailViewController()
            detailVC.gameId = gameID
            self.navigationController?.pushViewController(detailVC, animated: true)
        }
        cell.delCommitBlock = { commitID in 
            let moreView = ClubMyCommitMoreView()
            moreView.commitID = commitID
            moreView.delBlock = {
                self.delCommit(commitID: commitID)
            }
            self.entryAnimate.animation(show: moreView)
        }
        return cell
    }
    
}
